package com.fzf.blog.modules.api.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.fzf.blog.core.util.FcContants;
import com.fzf.blog.modules.admin.mapper.ResourceMapper;
import com.fzf.blog.modules.admin.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fzf.blog.core.util.ReType;
import com.fzf.blog.modules.admin.model.RResource;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * 资源分享
 *
 * @author fzf
 */
@RestController
@RequestMapping("/resource")
public class ApiResourceController {

	@Autowired
	private ResourceService resourceService;
    @Autowired
    private ResourceMapper resourceMapper;

	@GetMapping("/list")
	public ReType getList(String searchWord,Integer page, Integer limit) {
	    if(limit>54){
            limit=54;
        }
        Map<String, List<RResource>> maps = new HashMap<>();
        List<RResource> resources = new ArrayList<>();
        if (null != searchWord && !"".equals(searchWord)) {
            resources = resourceService.selectBySearchWord(searchWord);
        }else{
            resources = resourceService.selectAll(page,limit);
        }
        List<RResource> XM = resources.stream().filter(RResource -> RResource.getType() == FcContants.ZXXM||
                RResource.getType()==FcContants.JDXM)
                .collect(Collectors.toList());
        List<RResource> JC = resources.stream().filter(RResource -> RResource.getType() == FcContants.JCJC||
                RResource.getType()==FcContants.JCGJ)
                .collect(Collectors.toList());
        List<RResource> GJ = resources.stream().filter(RResource -> RResource.getType() == FcContants.GJJC||
                RResource.getType()==FcContants.GJGJ)
                .collect(Collectors.toList());
        List<RResource> QT = resources.stream().filter(RResource -> RResource.getType() == FcContants.QTZY)
                .collect(Collectors.toList());
        maps.put("XM", XM);
        maps.put("JC", JC);
        maps.put("GJ", GJ);
        maps.put("QT", QT);
		return new ReType(maps);
	}

	@GetMapping("/getDetail/{code}")
	public ReType getDetail(@PathVariable("code") Integer code) {
        RResource resource= resourceMapper.selectByPrimaryKey(code);
		return new ReType(resource);
	}

}
